package com.start.dataSource.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.start.common.api.Result;
import com.start.core.annotations.RegisterMenu;
import com.start.core.annotations.RegisterPermission;
import com.start.dataSource.entity.StartDataSource;
import com.start.dataSource.service.StartDataSourceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Tag(name = "数据源管理")
@RestController
@RequestMapping("/start/dataSource")
@RegisterMenu(value = "数据源管理", systemCode = "start:dataSource", component = "views/start/dataSource/index", path = "/mappers/start/dataSource/index")
public class StartDataSourceController {
    @Autowired
    private StartDataSourceService sysDataSourceService;

    @Operation(summary = "分页查询数据源")
    @GetMapping("/page")
    @PreAuthorize("@pas.hasPermission('start:dataSource:list')")
    public Result<?> page(Page page, StartDataSource entity) {
        LambdaQueryWrapper<StartDataSource> lambdaQuery = Wrappers.lambdaQuery(entity);
        return Result.ok(sysDataSourceService.page(page, lambdaQuery));
    }

    @Operation(summary = "添加数据源")
    @PostMapping("/add")
    @PreAuthorize("@pas.hasPermission('start:dataSource:add')")
    @RegisterPermission(value = "start:dataSource:add", name = "添加")
    public Result<?> add(@RequestBody StartDataSource entity) {
        sysDataSourceService.saveDs(entity);
        return Result.ok();
    }

    @Operation(summary = "修改数据源")
    @PutMapping("/updateById")
    @PreAuthorize("@pas.hasPermission('start:dataSource:edit')")
    @RegisterPermission(value = "start:dataSource:edit", name = "修改")
    public Result<?> updateById(@RequestBody StartDataSource entity) {
        sysDataSourceService.editorDs(entity);
        return Result.ok();
    }

    @Operation(summary = "获取数据源详情")
    @GetMapping("/getById/{id}")
    @PreAuthorize("@pas.hasPermission('start:dataSource:info')")
    @RegisterPermission(value = "start:dataSource:info", name = "查看")
    public Result<?> getById(@PathVariable("id") String id) {
        return Result.ok(sysDataSourceService.getById(id));
    }

    @Operation(summary = "删除数据源")
    @DeleteMapping("deleteById/{id}")
    @PreAuthorize("@pas.hasPermission('start:dataSource:delete')")
    public Result<?> deleteById(@PathVariable("id") String id) {
        sysDataSourceService.deleteDs(id);
        return Result.ok();
    }

    @Operation(summary = "测试数据源")
    @PostMapping("/testDataSource")
    @PreAuthorize("@pas.hasPermission('start:dataSource:testDataSource')")
    @RegisterPermission(value = "start:dataSource:testDataSource", name = "测试数据源")
    public Result<?> testDataSource(@RequestBody StartDataSource dataSource) {
        sysDataSourceService.testDataSource(dataSource.getName(), dataSource.getUrl(), dataSource.getUserName(), dataSource.getPwd());
        return Result.ok();
    }

    @Operation(summary = "获取全部数据源")
    @GetMapping("/list")
    @RegisterPermission(value = "start:dataSource:list", name = "查询全部数据源")
    @PreAuthorize("@pas.hasPermission('start:dataSource:list')")
    public Result<?> list() {
        return Result.ok(sysDataSourceService.list());
    }

}
